/*
 * =====================================================================================
 *
 *       Filename:  connectedComponents.cpp
 *
 *    Description:  Returns number of connected components
 *
 *        Version:  1.0
 *        Created:  16/07/12 11:00:58
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Moos Hueting (mh), mooshueting@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <cv.h>
#include <highgui.h>
#include <vector>

using namespace cv;
using namespace std;

float connectedComponents(Mat src) {
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Mat temp;
    src.convertTo(temp, CV_8UC1);

    findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

    unsigned int i;
    float nCC = 0;

    for (i=0; i < contours.size(); i++) {
        if (hierarchy[i][3] < 0) // contour without parents, thus not a hole
            nCC += 1;
    }

    return nCC;
}
